package com.zmzncs.lmtc.common.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zmzncs.lmtc.common.pojo.tengda.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 道尔
 */
@Slf4j
@Component
public class TengdaUtil {

    //  查询所有车辆类型
    public static String allCarTypes;
    @Value("${tengda.request-url.allCarTypes}")
    public void setAllCarTypes(String allCarTypes) {
        this.allCarTypes = allCarTypes;
    }


    //  查询所有通道信息
    public static String channels;
    @Value("${tengda.request-url.channels}")
    public void setChannels(String channels) {
        this.channels = channels;
    }

    //  添加月租
    public static String buyMonthCard;
    @Value("${tengda.request-url.buyMonthCard}")
    public void setBuyMonthCard(String buyMonthCard) {
        this.buyMonthCard = buyMonthCard;
    }

    //  续月租
    public static String monthCardRenewal;
    @Value("${tengda.request-url.monthCardRenewal}")
    public void setMonthCardRenewal(String monthCardRenewal) {
        this.monthCardRenewal = monthCardRenewal;
    }

    //  查询车辆月租信息
    public static String monthCard;
    @Value("${tengda.request-url.monthCard}")
    public void setMonthCard(String monthCard) {
        this.monthCard = monthCard;
    }

    public static String gate;
    @Value(("${tengda.request-url.gateOnOff}"))
    public void setGate(String gate){
        this.gate = gate;
    }

    public static String subscribe;
    @Value(("${tengda.request-url.subscribe}"))
    public void setSubscribe(String subscribe){
        this.subscribe = subscribe;
    }


    /**
     * 道闸抬杆落杆
     * @param bolianParkId 车场Id
     * @param channelId 通道ID
     * @param status 1开启 2 关闭
     * @param opt 操作人姓名
     * @return
     */
    public static Boolean gateOnOff(String bolianParkId, String channelId, Integer status, String opt){
        //关闭
        String action = "closegate";
        if (status == 1){
            //开启
            action = "opengate";
        }

        Map<String, String> map = new HashMap<>();
        map.put("bolianParkId",bolianParkId);
        map.put("channelId",channelId);
        map.put("action",action);
        map.put("opt",opt);

        String toJson = JsonUtil.objectToJson(map);
        log.info("请求参数：" + toJson);

        String retStr = HttpUtil.requestPostBody(gate, toJson, null);
        log.info("请求返回数据：" + retStr);
        TengdaForm form = JSONObject.parseObject(retStr, TengdaForm.class);
        if (form != null && form.getCode() == 1){
            log.info("道闸抬杆落杆成功，bolianParkId：" + bolianParkId);
            return true;
        } else {
            log.error("道闸抬杆落杆失败，bolianParkId：" + bolianParkId);
        }
        return false;
    }


    /**
     * 查询所有车辆类型
     */
    public static List<CarTypes> getCarTypes(String bolianParkId){
        List<CarTypes> carTypesList = new ArrayList<>();

        Map<String, String> map = new HashMap<>();
        map.put("bolianParkId",bolianParkId);
        log.info("腾达车辆类型请求参数：" + map.toString());
        String retStr = HttpUtil.requestGet(allCarTypes, map, null);
        log.info("腾达车辆类型响应数据：" + retStr);

        TengdaForm form = JSONObject.parseObject(retStr, TengdaForm.class);
        if (form != null && form.getCode() == 1){
            List<JSON> dataItems = form.getDataItems();
            if (dataItems != null && dataItems.size() > 0){
                for (JSON item : dataItems) {
                    CarTypes carTypes = JSONObject.toJavaObject(item, CarTypes.class);
                    carTypesList.add(carTypes);
                }
            }
        }

        return carTypesList;
    }

    /**
     * 续月租
     */
    public static Boolean renewalMonthCard(MonthCardForm form){

        Map<String, String> map = new HashMap<>();
        map.put("uuid",form.getUuid());
        map.put("bolianParkId",form.getBolianParkId());
        map.put("carNum",form.getCarNum());
        map.put("carTypeCode",form.getCarTypeCode().toString());
        map.put("carMMoney",form.getCarMMoney().toString());
        map.put("timeStart",form.getTimeStart());
        map.put("timeEnd",form.getTimeEnd());

        String toJson = JsonUtil.objectToJson(map);
        log.info("请求参数：" + toJson);

        String retStr = HttpUtil.requestPostBody(monthCardRenewal, toJson, null);
        log.info("请求返回数据：" + retStr);
        TengdaForm tengdaForm = JSONObject.parseObject(retStr, TengdaForm.class);
        if (tengdaForm != null && tengdaForm.getCode() == 1){
            log.info("查询历史订单成功，bolianParkId：" + form.getBolianParkId());
            return true;
        } else {
            log.error("查询历史订单失败，bolianParkId：" + form.getBolianParkId());
        }
        return false;
    }

    /**
     * 查询车辆月租信息
     */
    public static AllMonthCardInfo getCarMonthCard(String bolianParkId, String carNum){

        Map<String, String> map = new HashMap<>();
        map.put("bolianParkId",bolianParkId);
        map.put("carNum",carNum);
        log.info("请求参数：" + map.toString());

        String retStr = HttpUtil.requestGet(monthCard, map, null);
        log.info("请求返回数据：" + retStr);
        TengdaForm tengdaForm = JSONObject.parseObject(retStr, TengdaForm.class);
        if (tengdaForm != null && tengdaForm.getCode() == 1){
            log.info("查询车辆月租信息成功，bolianParkId：" + bolianParkId);
            List<JSON> dataItems = tengdaForm.getDataItems();
            if (dataItems != null && dataItems.size() > 0){
                for (JSON item : dataItems) {
                    AllMonthCardInfo monthCardInfo = JSONObject.toJavaObject(item, AllMonthCardInfo.class);
                    return monthCardInfo;
                }
            }
        } else {
            log.error("查询车辆月租信息失败，bolianParkId：" + bolianParkId);
        }
        return null;
    }


    /**
     * 添加月租
     */
    public static Boolean addMonthCard(MonthCardForm form){

        Map<String, String> map = new HashMap<>();
        map.put("bolianParkId",form.getBolianParkId());
        map.put("carNum",form.getCarNum());
        map.put("carTypeCode",form.getCarTypeCode().toString());
        map.put("carMMoney",form.getCarMMoney().toString());
        map.put("timeStart",form.getTimeStart());
        map.put("timeEnd",form.getTimeEnd());

        log.info("请求参数：" + map.toString());

        String retStr = HttpUtil.requestPostForm(buyMonthCard, map, null);
        log.info("请求返回数据：" + retStr);
        TengdaForm tengdaForm = JSONObject.parseObject(retStr, TengdaForm.class);
        if (tengdaForm != null && tengdaForm.getCode() == 1){
            log.info("查询历史订单成功，bolianParkId：" + form.getBolianParkId());
            return true;
        } else {
            log.error("查询历史订单失败，bolianParkId：" + form.getBolianParkId());
        }
        return false;
    }

    /**
     * 查询所有通道信息
     */
    public static List<AllChannels> getChannels(String bolianParkId){

        List<AllChannels> channelsList = new ArrayList<>();

        Map<String, String> map = new HashMap<>();
        map.put("bolianParkId",bolianParkId);
        log.info("请求参数：" + map.toString());

        String retStr = HttpUtil.requestGet(channels, map, null);
        log.info("请求返回数据：" + retStr);
        TengdaForm form = JSONObject.parseObject(retStr, TengdaForm.class);
        if (form != null && form.getCode() == 1){
            log.info("查询所有通道信息成功，bolianParkId：" + bolianParkId);
            List<JSON> dataItems = form.getDataItems();
            if (dataItems != null && dataItems.size() > 0){
                for (JSON item : dataItems) {
                    AllChannels channels = JSONObject.toJavaObject(item, AllChannels.class);
                    channelsList.add(channels);
                }
            }
        } else {
            log.error("查询所有通道信息失败，bolianParkId：" + bolianParkId);
        }
        return channelsList;
    }

    /**
     * 添加订阅停车场
     */
    public static Boolean subscribe(String bolianParkId){

        Map<String, String> map = new HashMap<>();
        map.put("bolianParkId",bolianParkId);
        log.info("请求参数：" + map.toString());

        String retStr = HttpUtil.requestGet(subscribe, map, null);
        log.info("请求返回数据：" + retStr);

        if (retStr != null){
            return true;
        } else {
            return false;
        }
    }


}
